package com.ysqcould.Community.service.impl;

import com.ysqcould.Community.domain.VolArea;
import com.ysqcould.Community.domain.vo.VolAreaTree;
import com.ysqcould.Community.mapper.VolAreaMapper;
import com.ysqcould.Community.service.IVolAreaService;
import com.ysqcould.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 义工区域Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-06-30
 */
@Service
public class VolAreaServiceImpl implements IVolAreaService 
{
    @Autowired
    private VolAreaMapper volAreaMapper;

    /**
     * 查询义工区域
     * 
     * @param id 义工区域主键
     * @return 义工区域
     */
    @Override
    public VolArea selectVolAreaById(Long id)
    {
        return volAreaMapper.selectVolAreaById(id);
    }

    /**
     * 查询义工区域列表
     * 
     * @param volArea 义工区域
     * @return 义工区域
     */
    @Override
    public List<VolArea> selectVolAreaList(VolArea volArea)
    {
        final List<VolArea> areas = volAreaMapper.selectVolAreaList(volArea);
        return  areas;
    }

    /**
     * 新增义工区域
     * 
     * @param volArea 义工区域
     * @return 结果
     */
    @Override
    public int insertVolArea(VolArea volArea)
    {
        return volAreaMapper.insertVolArea(volArea);
    }

    /**
     * 修改义工区域
     * 
     * @param volArea 义工区域
     * @return 结果
     */
    @Override
    public int updateVolArea(VolArea volArea)
    {
        return volAreaMapper.updateVolArea(volArea);
    }

    /**
     * 批量删除义工区域
     * 
     * @param ids 需要删除的义工区域主键
     * @return 结果
     */
    @Override
    public int deleteVolAreaByIds(Long[] ids)
    {
        return volAreaMapper.deleteVolAreaByIds(ids);
    }

    /**
     * 删除义工区域信息
     * 
     * @param id 义工区域主键
     * @return 结果
     */
    @Override
    public int deleteVolAreaById(Long id)
    {
        return volAreaMapper.deleteVolAreaById(id);
    }

    @Override
    public List<VolAreaTree> buildDeptTreeSelect(List<VolArea> depts) {
        List<VolArea> deptTrees = buildDeptTree(depts);
        return deptTrees.stream().map(VolAreaTree::new).collect(Collectors.toList());
    }

    private List<VolArea> buildDeptTree(List<VolArea> depts) {
        List<VolArea> returnList = new ArrayList<VolArea>();
        List<Long> tempList = new ArrayList<Long>();
        for (VolArea dept : depts)
        {
            tempList.add(dept.getId());
        }
        for (VolArea dept : depts)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getPid()))
            {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = depts;
        }
        return returnList;
    }

    private void recursionFn(List<VolArea> depts, VolArea dept) {
        // 得到子节点列表
        List<VolArea> childList = getChildList(depts, dept);
        dept.setChildren(childList);
        for (VolArea tChild : childList)
        {
            if (hasChild(depts, tChild))
            {
                recursionFn(depts, tChild);
            }
        }
    }

    private boolean hasChild(List<VolArea> depts, VolArea tChild) {
        return getChildList(depts, tChild).size() > 0;
    }

    private List<VolArea> getChildList(List<VolArea> depts, VolArea dept) {
        List<VolArea> tlist = new ArrayList<VolArea>();
        Iterator<VolArea> it = depts.iterator();
        while (it.hasNext())
        {
            VolArea n = (VolArea) it.next();
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == dept.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
